package com.demo.controller;



import com.demo.bean.Password;
import com.demo.bean.User;
import com.demo.common.Result;
import com.demo.service.ForumService;
import com.demo.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.Map;


@Controller
public class IndexController {
    @Autowired
    UserService userService;
    @Autowired
    ForumService forumService;

    @RequestMapping("/index")
    public String index(HttpServletRequest request,Model model){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if(user==null){
            model.addAttribute("msg","请先登录");
            return "redirect:/";
        }
        model.addAttribute("user",user);
        return "views/index";
    }



    @RequestMapping("/")
    public String toLogin(){
        return "views/user/login";
    }

    @RequestMapping("/loginCheck")
    public String loginCheck(User user,Model model,HttpServletRequest request){
        if(user.getLoginname()!=null&&!"".equals(user.getLoginname())){
           if(user.getPwd()!=null&&!"".equals(user.getPwd())) {
              User user1= userService.loginCheck(user);
              if(user1!=null){
                  HttpSession session = request.getSession();
                  session.setAttribute("user",user1);
                  return "redirect:/index";
              }

           }
        }
        model.addAttribute("msg","密码错误或者账号不存在");
            return "views/user/login";
    }

    @RequestMapping("toLogout")
    public String toLogout( HttpSession session){
        session.removeAttribute("user");
        return "redirect:/";
    }

//    跳转修改密码页面
    @RequestMapping("toChangePassWord")
    public String toChangePassWord(){
        return "views/set/user/password";
    }

    //    跳转修改密码页面
    @RequestMapping("toInfo")
    public String toInfo(HttpServletRequest request,Model model){
        User user = (User) request.getSession().getAttribute("user");
        model.addAttribute("item",user);
        return "views/set/user/info";
    }

    /**
     *修改密码
     * @param password
     * @param session
     * @return
     */
    @RequestMapping("changePassword")
    @ResponseBody
    public Result changePassword(@RequestBody Password password, HttpSession session){
        System.out.println(password);
        User user = (User) session.getAttribute("user");
        if(user.getPwd().equals(password.getOldPassword())){
            user.setPwd(password.getPassword());
            userService.updateUser(user);
            session.removeAttribute("user");
            return Result.ok();
        }else {
            return Result.error().msg("原始密码不正确，请确认后重试");
        }

    }

    /**
     *修改个人信息
     * @return
     */
    @RequestMapping("changeInfo")
    @ResponseBody
    public Result changeInfo(@RequestBody User user){
        userService.updateUser(user);
        return Result.ok();
    }

    /**
     *注册页面跳转
     * @return
     */
    @RequestMapping("toRegister")
    public String toRegister(){
        return "views/user/reg";
    }

    @RequestMapping("register")
    public String register(User user,Model model){
        System.out.println(user);
        User result=userService.getByloginname(user.getLoginname());
        if(result!=null){
            model.addAttribute("msg","用户名已被占用，请确认后重试");
            return "/toRegister";
        }else {
           userService.saveItem(user);
            return "/";
        }

    }
    /**
     *帖子详情页面跳转
     * @return
     */
    @RequestMapping("toDetail")
    public String toDetail(@RequestParam("id")String id,Model model){
        Map forum=forumService.getById(id);
        model.addAttribute("item",forum);
        return "views/forum/detail";
    }

    /**
     *新建帖子页面跳转
     * @return
     */
    @RequestMapping("toAddForum")
    public String toAddForum(HttpServletRequest request,Model model){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if(user==null){
            model.addAttribute("msg","请先登录");
            return "redirect:/";
        }
        return "views/forum/add";
    }

    /**
     *预览页面跳转
     * @return
     */
    @RequestMapping("/toPreview")
    public String toPreview(){
        return "views/piece/previewLayer";
    }

}
